package b3.widget.mail
{
	import b3.model.*;
	import b3.net.*;
	import b3.util.*;
	import b3.widget.*;
	import b3.widget.basic.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.filters.*;
	import flash.text.*;
	import flash.ui.*;

	public class MailDailog extends BasicWidget
	{
		private var toIdTextField:TextField;
		private var message:TextField;
		public var objectSlots:Array;
		private var sendButton:BasicButton;
		private static var instance:MailDailog;
		private var closeButton:BasicButton;
		private var currentSlotIndex:int;
		
		public function MailDailog()
		{
			super();
			this.anchor = ANCHOR_MIDDLE_MIDDLE;
			objectSlots = new Array(3);
			var Bgr:Class = ResourceHelper.getInstance().getClassByName("MessageBoxBgr");
			var bgr:Sprite = new Bgr();
			bgr.height = 350;
			bgr.scaleX = bgr.scaleY;
			bgr.mouseChildren = false;
			addChild(bgr);
			
			//标题
			var titleField:TextField = new TextField();
			titleField.selectable = false;
        	titleField.autoSize = TextFieldAutoSize.CENTER;
			titleField.defaultTextFormat = new TextFormat(BFont.fontName, 20, 0xFFFFFF, true);
			titleField.filters = [new GlowFilter(0x00AAFF, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			titleField.x = this.width / 2;
			titleField.y = 20;
			titleField.text = "撰写邮件";
			addChild(titleField);
			//idText
			var toIdLabel:TextField = new TextField();
        	toIdLabel.selectable = false;
        	toIdLabel.autoSize = TextFieldAutoSize.RIGHT;
			toIdLabel.defaultTextFormat = new TextFormat(BFont.fontName, 16, 0xFFFFFF, true);
			toIdLabel.filters = [new GlowFilter(0x00AAFF, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			toIdLabel.x = 140;
			toIdLabel.y = 80;
			toIdLabel.text = "发送ID:";
			addChild(toIdLabel);
			toIdTextField = new TextField();
			toIdTextField.type = TextFieldType.INPUT;
			toIdTextField.defaultTextFormat = new TextFormat(BFont.fontName, 16, 0x000000);
			toIdTextField.background = true;
			toIdTextField.backgroundColor = 0xFFFFFF;
			toIdTextField.x = 160;
			toIdTextField.y = 80;
			toIdTextField.width = 270;
			toIdTextField.height = 25;
			addChild(toIdTextField);
			
			//消息
			var messageLabel:TextField = new TextField();
			messageLabel.selectable = false;
        	messageLabel.autoSize = TextFieldAutoSize.RIGHT;
			messageLabel.defaultTextFormat = new TextFormat(BFont.fontName, 16, 0xFFFFFF, true);
			messageLabel.filters = [new GlowFilter(0x00AAFF, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			messageLabel.x = 140;
			messageLabel.y = 120;
			messageLabel.text = "发送消息:";
			addChild(messageLabel);
			message = new TextField();
			message.type = TextFieldType.INPUT;
        	message.wordWrap = true;
        	message.width = 270;
			message.height = 55;
			message.maxChars = 30;
			message.background = true;
			message.backgroundColor = 0xFFFFFF;
			message.defaultTextFormat = new TextFormat(BFont.fontName, 16, 0x000000);
			message.text = "你好！送你一点小礼物哦~一起加油建设家园吧~";
			message.x = 160;
			message.y = 120;
			addChild(message);
			
			//附件
			var attachmentLabel:TextField = new TextField();
			attachmentLabel.selectable = false;
        	attachmentLabel.autoSize = TextFieldAutoSize.RIGHT;
			attachmentLabel.defaultTextFormat = new TextFormat(BFont.fontName, 16, 0xFFFFFF, true);
			attachmentLabel.filters = [new GlowFilter(0x00AAFF, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			attachmentLabel.x = 140;
			attachmentLabel.y = 190;
			attachmentLabel.text = "附件礼物:";
			addChild(attachmentLabel);
			//物品框
			currentSlotIndex = 0;
			for (var i:int = 0;i < objectSlots.length;i++)
			{
				var objectSlot:ObjectSlot = new ObjectSlot();
				objectSlot.x = 160 + i * 90;
				objectSlot.y = 190;
				addChild(objectSlot);
				objectSlots[i] = objectSlot;
				objectSlot.mouseChildren = false;
				objectSlot.addEventListener(MouseEvent.MOUSE_OVER, onObjectSlotMouseOver);
				objectSlot.addEventListener(MouseEvent.MOUSE_OUT, onObjectSlotMouseOut);
				objectSlot.addEventListener(MouseEvent.CLICK, onObjectSlotClick);
			}
			
			//发送按钮
			var ButtonBgr:Class = ResourceHelper.getInstance().getClassByName("ButtonBgr0");
			sendButton = new BasicButton(ButtonBgr, "发送");
			sendButton.x = 115;
			sendButton.y = 270;
			addChild(sendButton);
			sendButton.addEventListener(MouseEvent.CLICK, onSendButtonClick);
			
			//关闭按钮
			closeButton = new BasicButton(ButtonBgr, "取消");
			closeButton.x = 215;
			closeButton.y = 270;
			addChild(closeButton);
			closeButton.addEventListener(MouseEvent.CLICK, onCloseClick);
			
			//清除按钮
			var clearButton:BasicButton = new BasicButton(ButtonBgr,"清除");
			clearButton.x = 315;
			clearButton.y = 270;
			addChild(clearButton);
			clearButton.addEventListener(MouseEvent.CLICK, onClearClick);
		}
		
		private function onObjectSlotMouseOver(event:MouseEvent):void
		{
			Mouse.cursor = "button";
		}    
		
		private function onObjectSlotMouseOut(event:MouseEvent):void
		{
			Mouse.cursor = "auto";
		}
		
		private function onObjectSlotClick(event:MouseEvent):void
		{
			var mailTypePane:MailTypePane = new MailTypePane();
			WidgetManager.getInstance().popOut([mailTypePane], true);
		}
		
		private function onSendButtonClick(event:MouseEvent):void
		{
			//准备附件
			var objArray:Array = new Array();
			for (var i:int = 0;i < objectSlots.length; i++)
			{
				var objectSlot:ObjectSlot = objectSlots[i];
				if (objectSlot.obj != null)
					objArray.push(objectSlot.obj);
			}
			//发送邮件
			var me:Player = PlayerManager.getInstance().me;
			var mail:MailModel = new MailModel();
			mail.fromName = (me.type == PlayerManager.MANAGER_TYPE ? "神马管理员" : me.name);
			mail.fromId = me.userId;
			mail.toId = toIdTextField.text;
			mail.message = message.text;
			mail.gifts = objArray;
			mail.send();
			mail.addEventListener(ModelEvent.SEND_MAIL_COMPLETE, onSendMailComplete);
		}
		
		private function onSendMailComplete(event:ModelEvent):void
		{
			var popMessage:PopMessage;
			var data:Object = event.data;
			if (data['err'] == 0)
			{
				popMessage = new PopMessage("邮件发送成功！", 0x00AA00);
				WidgetManager.getInstance().popOut([popMessage]);
				close();	
			}
			else
			{
				if (data['err'] == 101)
				{
					popMessage = new PopMessage("收件人不存在！", 0xFF0000);	
				}
				else
				{
					popMessage = new PopMessage("未知错误！" + data['err'], 0xFF0000);
				}
				WidgetManager.getInstance().popOut([popMessage]);
			}
		}
		
		private function onCloseClick(event:Event):void
		{
			close();
		}
		
		private function onClearClick(event:Event):void
		{
			clear();
		}
		
		private function clear():void
		{
			for (var i:int = 0;i < objectSlots.length;i++)
			{
				var objectSlot:ObjectSlot = objectSlots[i];
				objectSlot.removeObj();
			}
			currentSlotIndex = 0;
			message.text = "";
			toIdTextField.text = "";
		}
		
		public function addOjbect(obj:Obj):void
		{
			if (currentSlotIndex < 3)
			{
				var objectSlot:ObjectSlot = objectSlots[currentSlotIndex];
				objectSlot.addOjbect(obj);
				currentSlotIndex ++;
			}
		}
	}
}